package com.dc.modules.main.controller;

import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.dc.commons.json.JsonWrapper;
import com.dc.commons.views.ModelAndViewBuilder;

@ControllerAdvice
public class DefaultExceptionHandler {

	Logger logger=LoggerFactory.getLogger(DefaultExceptionHandler.class);
	
	 @ExceptionHandler({UnauthorizedException.class})
	 @ResponseStatus(HttpStatus.UNAUTHORIZED)
	 public ModelAndView processUnauthorizedException(NativeWebRequest request,UnauthorizedException e){
		 logger.error("报错了",e);
 		return  ModelAndViewBuilder
 					.create()
 					.error(e.getMessage(),e)
 					.viewName("exception/unauthorized")
 					.build();
	 }
	 
	 
	 @ExceptionHandler({AuthorizationException.class})
	 public ModelAndView processAuthorizationException(NativeWebRequest request,AuthorizationException e){
		 logger.error("报错了",e);
 		return  ModelAndViewBuilder
					.create()
					.json(JsonWrapper.unAuthorized(e.getMessage()))
					.viewName("exception/authorization")
					.build();
	 }
	 
	 
	 @ExceptionHandler({ExpiredCredentialsException.class})
	 public ModelAndView processExpiredCredentialsException(NativeWebRequest request,Exception e){
		 logger.error("报错了",e);
		 return  ModelAndViewBuilder
					.create()
					.error(e.getMessage(),e)
					.viewName("exception/expiredCredentials")
					.build();
	 }
	 
	 
	 @ExceptionHandler({Exception.class})
	 public ModelAndView processException(NativeWebRequest request,Exception e){
		 
		 return  ModelAndViewBuilder
					.create()
					.error(e.getMessage(),e)
					.viewName("exception/exception")
					.build();
	 }
}
